package com.fjwt.gz.mch.ctrl.coupon;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.PointDTO;
import com.fjwt.gz.db.vo.FansCouponsDetailVo;
import com.fjwt.gz.db.vo.FansCouponsVo;
import com.fjwt.gz.db.vo.NearMchListVo;
import com.fjwt.gz.mch.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.AwardCouponService;
import com.fjwt.gz.service.impl.FansAwardService;
import com.fjwt.gz.service.impl.MchInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/api/award/coupon")
public class AwardCouponController extends CommonCtrl {
    @Autowired
    private FansAwardService fansAwardService;
    @Autowired
    private AwardCouponService awardCouponService;
    @Autowired
    private MchInfoService mchInfoService;


    /**
     * 福券列表  奖品状态 0：未使用  1：已使用 2：已过期
     */
    @PostMapping("/getcouponList")
    public ApiRes getcouponList() {
        Long fansId = getCurrentUser().getFansId();
        Long couponState = getValLong("couponState");
        Long awardType = getValLong("awardType");
        Long virtualType = getValLong("virtualType");
        Long virtualState = getValLong("virtualState");
        String appId = request.getHeader("appId");
        log.info("C端用户卡券列表接口调用，请求地址：/api/award/coupon/getcouponList，请求参数：用户fansId：{}，状态：{}，类型：{},虚拟类型：{},虚拟状态：{},appid{}",fansId,couponState,awardType,virtualType,virtualState,appId);
        IPage<FansCouponsVo> page = fansAwardService.selectListBycouponState(getIPage(), fansId, couponState, awardType, virtualType, virtualState, appId);
        log.info("C端用户卡券列表接口调用，请求地址：/api/award/coupon/getcouponList，返回结果：{}", JSONUtil.toJsonStr(page));
        return ApiRes.page(page);
    }

    /**
     *
     * 查询卡券奖品详情
     * @return
     */
    @RequestMapping(value = "/details", method = RequestMethod.POST)
    public ApiRes details() {
        Long couponId = getValLong("couponId");
        Long fansAwardId = getValLong("fansAwardId");
        Long goodsId = getValLong("goodsId");
        Long virtualAwardId = getValLong("virtualAwardId");
        log.info("C端用户奖品详情接口调用，请求地址：/api/award/coupon/details，请求参数：用户奖品id：{}，卡券id：{}，实物id：{}",fansAwardId,couponId,goodsId);
        FansCouponsDetailVo vo = awardCouponService.getFansCouponDetail(couponId,goodsId,fansAwardId, virtualAwardId);
        log.info("C端用户奖品详情接口调用，请求地址：/api/award/coupon/details，返回结果：{}", JSONUtil.toJsonStr(vo));
        return ApiRes.ok(vo);
    }

    /**查询卡券奖品详情**/
    @PostMapping("/getMchInfoDestroyList")
    public ApiRes getNearMchList() {

        PointDTO location = getVal("location", PointDTO.class);
        String radius = getValString("radius");
        Long couponId = getValLong("couponId");
        log.info("C端卡券导航到使用门店接口调用，请求地址：/api/award/coupon/getMchInfoDestroyList，请求参数：卡券id：{}，位置{}，范围单位（米）：{}",couponId,location,radius);
        IPage<NearMchListVo> page = mchInfoService.getNearMchList(getIPage(), location, radius, couponId);
        log.info("C端卡券导航到使用门店接口调用，请求地址：/api/award/coupon/getMchInfoDestroyList，返回结果：{}", JSONUtil.toJsonStr(page));
        return ApiRes.page(page);
    }



}
